import { ComponentClass } from 'react'
import { connect } from '@tarojs/redux'

import Taro, { Component, Config } from '@tarojs/taro'

import { AtLoadMore } from 'taro-ui'
import { View } from '@tarojs/components'

import HomeShare from '../../containers/HomeShare'
import HomeContent from '../../containers/HomeContent'
import HomeHandle from '../../containers/HomeHandle'
import XPage from '../../components/XPage'

import { queryList } from '../../actions/goods'

import "./testImg.scss"

type PageStateProps = {
  goods: object | {},
}

type PageDispatchProps = {
  fetchList: (object) => void
}

type PageOwnProps = {}

type PageState = {}

type IProps = PageStateProps & PageDispatchProps & PageOwnProps

interface Home {
  props: IProps
}

@connect(({ goods }) => ({
  goods
}), (dispatch) => ({
  fetchList(params) {
    dispatch(queryList(params))
  }
}))
class Home extends Component {
  config: Config = {
    navigationBarTitleText: '幸运草小程序'
  }

  componentWillMount() {
    // 初始化数据
    this.props.fetchList({type: 1, sorter: 'id_ascend'})
  }

  //分享按钮函数
  onShareAppMessage() {
    return {
      title: '幸运草小程序',
      path: 'pages/home/index?userId=0',
      success(res) {
          console.log("转发成功:" + JSON.stringify(res))
      },
      fail(res) {
          console.log("转发失败:" + JSON.stringify(res))
      }
    }
  }

  handleToPage = (params, msg) => {
    msg.stopPropagation()

    Taro.navigateTo({
      url: `/pages/profile/index?id=${params.id}`
    })
  }

  render() {
    return (
      <View class="home-page">
        <HomeShare />
        <View class="home-list">
          {
            this.props.goods.isLoading
            ?
            <AtLoadMore loadingText='加载中...' status={this.props.goods.isLoading ? 'loading' : 'noMore'} />
            :
            this.props.goods.data.data.map((row) => <HomeContent  queryRow={row} key={row.id} onClick={this.handleToPage.bind(this, row)} />)
          }          
        </View>
        <HomeHandle />
      </View>
    )
  }
}

export default Home as ComponentClass<PageOwnProps, PageState>